<template>
  <div class="app-container">
    <!--    <el-row :gutter="20">-->
    <!--部门数据-->
    <!--      <el-col :span="4" :xs="24">-->
    <!--        <div class="head-container">-->
    <!--          <el-input-->
    <!--              v-model="deptName"-->
    <!--              placeholder="请输入部门名称"-->
    <!--              clearable-->
    <!--              size="small"-->
    <!--              prefix-icon="el-icon-search"-->
    <!--              style="margin-bottom: 20px"-->
    <!--          />-->
    <!--        </div>-->
    <!--        <div class="head-container">-->
    <!--          <el-tree-->
    <!--              :data="deptOptions"-->
    <!--              :props="defaultProps"-->
    <!--              :expand-on-click-node="false"-->
    <!--              :filter-node-method="filterNode"-->
    <!--              ref="tree"-->
    <!--              node-key="id"-->
    <!--              default-expand-all-->
    <!--              highlight-current-->
    <!--              @node-click="handleNodeClick"-->
    <!--          />-->
    <!--        </div>-->
    <!--      </el-col>-->
    <el-col >
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <!--          <el-form-item label="医生编号" prop="userId">-->
        <!--            <el-input-->
        <!--                v-model="queryParams.userId"-->
        <!--                placeholder="请输入医生编号"-->
        <!--                clearable-->
        <!--                @keyup.enter.native="handleQuery"-->
        <!--            />-->
        <!--          </el-form-item>-->
        <!--          <el-form-item label="科室编号" prop="deptId">-->
        <!--            <el-input-->
        <!--                v-model="queryParams.deptId"-->
        <!--                placeholder="请输入科室编号"-->
        <!--                clearable-->
        <!--                @keyup.enter.native="handleQuery"-->
        <!--            />-->
        <!--          </el-form-item>-->
        <el-form-item label="患者编号" prop="createId">
          <el-input
            v-model="queryParams.createId"
            placeholder="请输入患者编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="挂号状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择挂号状态" clearable>
            <el-option
              v-for="dict in dict.type.cys_registered_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!--        <el-row :gutter="10" class="mb8">-->
      <!--          <el-col :span="1.5">-->
      <!--            <el-button-->
      <!--                type="primary"-->
      <!--                plain-->
      <!--                icon="el-icon-plus"-->
      <!--                size="mini"-->
      <!--                @click="handleAdd"-->
      <!--                v-hasPermi="['registered:registered:add']"-->
      <!--            >挂号</el-button>-->
      <!--          </el-col>-->
      <!--          <el-col :span="1.5">-->
                  <el-button
                      type="warning"
                      plain
                      icon="el-icon-"
                      size="mini"
                      :disabled="multiple"
                      @click="handleChange"
                      v-hasPermi="['registered:registered:change']"
                  >处理</el-button>
      <!--          </el-col>-->
      <!--          <el-col :span="1.5">-->
      <!--            <el-button-->
      <!--              type="warning"-->
      <!--              plain-->
      <!--              icon="el-icon-download"-->
      <!--              size="mini"-->
      <!--              @click="handleExport"-->
      <!--              v-hasPermi="['registered:registered:export']"-->
      <!--            >导出</el-button>-->
      <!--          </el-col>-->
      <!--          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
      <!--        </el-row>-->

      <el-table v-loading="loading" :data="registeredList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="号码" align="center" prop="registeredId" />
        <el-table-column label="患者编号" align="center" prop="createId" />
         <el-table-column label="医生" align="center" prop="userName" />
        <el-table-column label="科室" align="center" prop="deptName" />
        <el-table-column label="患者" align="center" prop="patientName" />
        <el-table-column label="挂号状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.cys_registered_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
<!--                  <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--                    <template slot-scope="scope">-->
<!--                      <el-button-->
<!--                        size="mini"-->
<!--                        type="text"-->
<!--                        icon="el-icon-edit"-->
<!--                        @click="handleUpdate(scope.row)"-->
<!--                        v-hasPermi="['registered:registered:edit']"-->
<!--                      >处理</el-button>-->
        <!--              <el-button-->
        <!--                  size="mini"-->
        <!--                  type="text"-->
        <!--                  icon="el-icon-"-->
        <!--                  @click="handleDelete(scope.row)"-->
        <!--                  v-hasPermi="['registered:registered:remove']"-->
        <!--              >退号</el-button>-->
<!--                    </template>-->
<!--                  </el-table-column>-->
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-col>
    <!--    </el-row>-->

    <!--     添加或修改挂号对话框-->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--        <el-form-item label="选择科室" prop="deptId">-->
        <!--          <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="医生编号">-->
        <!--          <el-select v-model="form.userIds" multiple placeholder="请选择医生">-->
        <!--            <el-option-->
        <!--                v-for="item in userOptions"-->
        <!--                :key="item.userId"-->
        <!--                :label="item.userName"-->
        <!--                :value="item.userId"-->
        <!--                :disabled="item.status == 1"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="患者编号" prop="createId">-->
        <!--          <el-input v-model="form.createId" placeholder="请输入患者编号" />-->
        <!--        </el-form-item>-->
        <el-form-item label="挂号状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.cys_registered_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRegistered, getRegistered, getRegistered1, delRegistered, addRegistered, updateRegistered, deptTreeSelect, changeRegistered} from "@/api/viewRegistered/viewRegistered";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {parseTime} from "../../../utils/ruoyi";
export default {
  name: "Registered",
  components: { Treeselect },
  dicts: ['cys_registered_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 挂号表格数据
      registeredList: [],
      // 弹出层标题
      title: "",
      // 部门名称
      deptName: undefined,
      // 部门树选项
      deptOptions: undefined,
      //部门ID
      deptID : undefined,
      // 医生选择
      userOptions: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        deptId: null,
        createId: null,
        status: null
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {

      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    parseTime,
    /** 查询挂号列表 */
    getList() {
      this.loading = true;
      listRegistered(this.queryParams).then(response => {
        this.registeredList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        registeredId: null,
        userId: null,
        deptId: null,
        createId: null,
        createTime: null,
        status: null,
        userIds: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.deptId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.registeredId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getRegistered1(this.queryParams.deptId).then(response => {
        this.userOptions = response.users;
        this.open = true;
        this.title = "添加挂号";
      });


    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const registeredId = row.registeredId || this.ids
      getRegistered(registeredId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改挂号";
      });
    },
    /**处理挂号状态为已完成*/
    handleChange(row){
      const registeredIds = row.registeredId || this.ids
      this.$modal.confirm('是否处理号码为"' + registeredIds + '"的挂号？').then(function (){
        return changeRegistered(registeredIds);
      }).then(() =>{
        this.getList();
        this.$modal.msgSuccess("处理完成");
      }).catch(() => {})
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const registeredIds = row.registeredId || this.ids;
      this.$modal.confirm('是否确认退号号码为"' + registeredIds + '"的挂号？').then(function() {
        return delRegistered(registeredIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("退号成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.registeredId != null) {
            updateRegistered(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRegistered(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },



    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },

    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      // this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('registered/registered/export', {
        ...this.queryParams
      }, `registered_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
